// pages/mainOperation/my/my.js
const app = getApp();
var utils = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    idCardState: false, //身份证认证状态   true 已认证      false 未认证
    idFaceState: false, //人脸识别状态    true 已认证      false 未认证
    userHead: '../../images/my/cgt-head-icon.jpg',
    userName: '登录/注册', //用户姓名
    userMobile: '', //用户手机
    userIDdNum: '' //身份证号码
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let self = this;

    switch (app.userInfo.AuthLevel) {
      case 0: //注册未认证
        self.setData({
          idCardState: false, //身份证认证状态   true 已认证      false 未认证
          idFaceState: false //人脸识别状态    true 已认证      false 未认证
        });
        break;
      case 2:
      case 3:
        self.setData({
          idCardState: true, //身份证认证状态   true 已认证      false 未认证
          idFaceState: false //人脸识别状态    true 已认证      false 未认证
        });
        break;
      case 4:
        self.setData({
          idCardState: true, //身份证认证状态   true 已认证      false 未认证
          idFaceState: true //人脸识别状态    true 已认证      false 未认证
        });
        break;
    }

    if (app.userInfo.UserID) {
      self.setData({
        userMobile: utils.hideMobile(app.userInfo.UserMobile),
        userName: app.userInfo.ThirdUserName || '',
        userHead: app.userInfo.ThirdUserHead || '../../images/my/cgt-head-icon.jpg'
      })
    }
  },
  /**
   * 生命周期函数--页面显示
   */
  onShow: function() {
    let self = this;
    if (app.userInfo.UserID && app.userInfo.AuthLevel > 0) {
      let url = app.apiBaseUrl + '/User/GetIDCard';
      app.httpRequest({
        url: url,
        showLoading: false,
        method: "GET",
        data: {
          userId: app.userInfo.UserID
        },
        success: function(res) {
          let rdata = res.data;
          if (rdata.status == 0) {
            self.setData({
              userIDdNum: rdata.data.IDNum
            });
            if (utils.isNullOrEmpty(app.userInfo.ThirdUserName)) {
              self.setData({
                userName: rdata.data.UserName
              });
            }
          } else {
            wx.showToast({
              title: '获取用户验证信息失败',
              icon: 'none'
            });
          }
        }
      });
    }
  },
  /**
   *  消息跳转
   */
  goToMsgTap: function() {
    let self = this;
    if (app.userInfo.UserID) {
      wx.navigateTo({
        url: 'message/messageList/messageList',
      })
    } else {
      wx.showToast({
        title: '用户未认证，请点击头像进行认证',
      })
    }
  },
  /**
   *  收件地址跳转
   */
  goToAddressTap: function() {
    if (app.userInfo.UserID) {
      wx.navigateTo({
        url: 'myAddress/myAddressList/myAddressList',
      })
    } else {
      wx.showToast({
        title: '用户未认证，请点击头像进行认证',
      })
    }
  },
  /**
   *  用户绑定
   */
  bindUserTap: function(e) {
    if (app.userInfo.UserID <= 0) {
      wx.navigateTo({
        url: '/pages/liveAuthentication/sms/sms',
      })
    } else {
      //用户未认证-手机认证
      if (app.userInfo.AuthLevel == 0) {
        wx.navigateTo({
          url: '/pages/liveAuthentication/ocr/ocr',
        })
        console.log('usr.AuthLevel0', usr.AuthLevel);
      } else if (app.userInfo.AuthLevel < 4) { //身份认证
        wx.navigateTo({
          url: '/pages/liveAuthentication/video/video',
        })
        console.log('usr.AuthLevel4', usr.AuthLevel);
      }
    }
  },
  /**
   *  获取用户信息
   */
  getUserInfo: function(e) {
    let self = this;
    console.log("app.userInfo.UserID:0:" + app.userInfo.UserID)
    if (app.userInfo.UserID) {
      if (utils.isNullOrEmpty(app.userInfo.ThirdUserHead)) {
        if (e.detail.errMsg == 'getUserInfo:ok') {
          let userInfo = e.detail.userInfo;
          self.updUserHead(userInfo);
        } else {
          //没有授权获取昵称和头像就先进行授权
          wx.showToast({
            title: '用户没有授权获取昵称和头像,获取头像失败',
            icon: 'none',
            duration: 2000
          })
        }
      }
    } else {
      if (e.detail.errMsg == 'getUserInfo:ok') {
        let userInfo = e.detail.userInfo;
        app.addUserAuth(function(usr) {
          self.updUserHead(userInfo);
          wx.navigateTo({
            url: '/pages/liveAuthentication/sms/sms',
          });
        });
      } else {
        //没有授权获取昵称和头像就先进行授权
        wx.showToast({
          title: '用户没有授权获取昵称和头像,获取头像失败',
          icon: 'none',
          duration: 2000
        })
      }
    }
  },
  updUserHead(userInfo) {
    let self = this;
    if (!utils.isNullOrEmpty(app.userInfo.ThirdUserHead)){
      return;
    }
    let url = app.apiBaseUrl + '/WxMini/UpdateUserHead';
    app.httpRequest({
      url: url,
      showLoading: false,
      method: "POST",
      data: {
        ThirdUserHead: userInfo.avatarUrl,
        ThirdUserName: userInfo.nickName,
        ThirdUserID: app.userInfo.ThirdUserID
      },
      success: function(res) {
        let rdata = res.data;
        if (rdata.status == 0) {
          app.userInfo.ThirdUserHead = userInfo.avatarUrl;
          app.userInfo.ThirdUserName = userInfo.nickName;

          self.setData({
            userHead: userInfo.avatarUrl,
            userName: userInfo.nickName
          })
        } else {
          wx.showToast({
            title: '头像绑定失败',
            icon: 'none'
          });
        }
      }
    });
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})